/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.bluelogic.dao;

import br.com.bluelogic.bean.Email;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.*;

/**
 *
 * @author Administrador
 */
public class eMailDAO implements InterfaceEmail{

    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static final String SMTP_PORT = "465";
    public boolean enviaEmail(Email mail) {
        try {
            Properties props = System.getProperties();            
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.socketFactory.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.put("mail.smtp.socketFactory.fallback", "false");            
            Authenticator auth = new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("colocar email", "colocar senha");
                }
            };
            Session session = Session.getDefaultInstance(props, auth);            
            session.setDebug(true);            
            Message msg = new MimeMessage(session);            
            try {
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress("email", ""));
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(eMailDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            msg.setSubject(mail.geteMail());
            msg.setText(mail.getNome()+"\n"+mail.getMensagem());
            Transport.send(msg);

            return true;
        } catch (MessagingException ex) {
            Logger.getLogger(eMailDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } 
    }

    public String confirmaEmail(String mail) {
        return "E-mail recebido com sucesso, obrigado";
    }
    
}
